package com.bjsdzk.collector.controller;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.bjsdzk.collector.core.Result;
import com.bjsdzk.collector.core.ResultGenerator;
import com.bjsdzk.collector.platform.coap.utils.JsonUtil;
import com.bjsdzk.collector.serviceImp.TelecomService;

@RestController
@RequestMapping("/teleco")
public class TelecomController {
	private static Logger logger = LoggerFactory.getLogger(TelecomController.class);
    
    
    @Autowired
    private TelecomService telecomService;
    
    @GetMapping("/getToken")
    public String getToken()  {
		return telecomService.getToken1();
	}
    
    @GetMapping("/getManualToken")
    public void getManualToken() {
    	try {
    		telecomService.getToken();
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("获取token出错");
		}
    }
    
    
    @GetMapping("/refreshToken")
    public String refreshToken() throws Exception  {
		return telecomService.refreshToken();
	}
    
    @GetMapping("/queryDeviceData")
    public String queryDeviceData(String deviceId)   {
    	String data ="";
		try {
			data=telecomService.queryDeviceData(deviceId);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return data;
	}
    @GetMapping("/queryDevices")
    public String queryDevices(String deviceId,String gatewayId)   {
    	String data ="";
		try {
			data=telecomService.queryDevices( deviceId,gatewayId);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return data;
	}
    @GetMapping("/queryDataHistory")
    public String queryDataHistory(String deviceId,String gatewayId,String pageNo,String pageSize,String startTime,String endTime)   {
    	String data ="";
		try {
			data=telecomService.queryDataHistory(deviceId, gatewayId, pageNo, pageSize, startTime, endTime);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return data;
	}
    @GetMapping("/subscribeNotify")
    public String subscribeNotify(String deviceId)   {
    	String data ="";
		try {
			data=telecomService.subscribeNotify();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return data;
	}
    

    @PostMapping("/regDirectDevice")
    public String regDirectDevice(String nodeId,String name,String location,Integer type)   {
		String verifyCode=nodeId;
		String data="";
		try {
			if(type==null) {
				data=telecomService.regDirectDevice(verifyCode,nodeId,name,location);
			}else {
				data=telecomService.regDirectrrrrrrDevice(verifyCode,nodeId,name,location);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return data;
	}
    
    @GetMapping("/queryDeviceStatus")
    public String queryDeviceStatus(String deviceId)   {
    	String data ="";
		try {
			data=telecomService.queryDeviceStatus(deviceId);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return data;
	}
    
    @GetMapping("/deleteDirectDevice")
    public String deleteDirectDevice(String deviceId)   {
    	String data ="";
		try {
			data=telecomService.deleteDirectDevice(deviceId);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return data;
	}
    
    @GetMapping("/lookAll")
    public String lookAll()   {
    	String data ="";
		try {
			data=telecomService.lookAll();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return data;
	}
    
    @GetMapping("/cmd")
    public String cmd(String deviceId,String serviceId,String method,int fDetectorAddress,int fDetectorType,int fCommandType,int fCheckSum,Integer fValue)   {
    	String data ="";
		try {
			data=telecomService.cmd(deviceId, serviceId, method, fDetectorAddress, fDetectorType, fCommandType, fValue);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return data;
	}
    
    @GetMapping("/queryCmd")
    public String queryCmd()   {
    	String data ="";
		try {
			data=telecomService.queryCmd();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return data;
	}
    
    @PostMapping("/reportCmdExecResult")
    @SuppressWarnings("unchecked")
    public Result deletedDevice(@RequestBody Object message){
        Map<String,String> data = new HashMap<>();
        data = JsonUtil.jsonString2SimpleObj(message.toString(), data.getClass());
        logger.debug("callback cmd"+message);
        return ResultGenerator.genSuccessResult(data);
    }
    
    
    
    
    @GetMapping("/test")
    public String test() throws Exception {
        String deviceId = "69f81e22-eeb6-4d14-8a26-0980499e94a7";
        String gatewayId = "69f81e22-eeb6-4d14-8a26-0980499e94a7";
        String verifyCode=UUID.randomUUID().toString();
        String nodeId="1410009249877";
        
        telecomService.queryDeviceData(deviceId);
    	String data ="";
			data = telecomService.queryDevices(deviceId,gatewayId);
			data =telecomService.queryDataHistory(deviceId,gatewayId,"1","2",null,null);
			data=telecomService.subscribeNotify();
			data=telecomService.regDirectDevice(verifyCode,nodeId,"","");
			JSONObject jsonObject = JSONObject.parseObject(data); 
			
			deviceId=jsonObject.get("deviceId").toString();
			System.out.println(deviceId);
			data=telecomService.queryDeviceStatus(deviceId);
			data=telecomService.deleteDirectDevice(deviceId);
			deviceId="69f81e22-eeb6-4d14-8a26-0980499e94a7";
			//data=telecomService.cmd(deviceId);
			data=telecomService.queryCmd();
		return data;
	}
    
	
    

}
